package com.apes.fn.scm.retailOrder.service;

import com.apes.fn.scm.retailOrder.repository.RetailOrderRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.account.account.model.AccountingMethod;
import com.apes.scm.account.account.model.InventoryMode;
import com.apes.scm.masterdata.product.service.UomService;
import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.retailOrder.model.RetailOrderItem;
import com.apes.scm.stock.transfer.model.ConsumableUse;
import com.apes.scm.stock.transfer.model.ConsumableUseItem;
import com.apes.scm.stock.transfer.model.Pick;
import com.apes.scm.stock.transfer.repository.ConsumableUseRepository;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherItem;
import com.apes.scm.voucher.service.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service("pickFlowRetailOrderVoucherService")
public class PickFlowRetailOrderVoucherService {

    @Autowired
    RetailOrderRepository retailOrderRepository;
    @Autowired
    ConsumableUseRepository consumableUseRepository;

    @Autowired
    UomService uomService;

    public Voucher flowRetailOrderVoucher(SimpleRequest simpleRequest) {
        Pick pick = simpleRequest.getO(Pick.class);
        RetailOrder retailOrder = retailOrderRepository.findOne(pick.getOrigin());
        if (retailOrder == null) throw new RuntimeException(pick.getOrigin() + "零售订单不存在！");
        Voucher voucher = new Voucher();
        // 主表信息
        voucher.setCompany(retailOrder.getCompany());
        voucher.setDept(retailOrder.getDept());
        voucher.setClerk(retailOrder.getClerk()); //业务员
        voucher.setPartner(retailOrder.getCompany().getParty().getPatyRoleByType("Customer"));

        voucher.setInventoryMode(InventoryMode.IM_OWN);     //设置库存模式：自有
        voucher.setAccountingMethod(AccountingMethod.AM_PURCHASE);  // 核算方式：经销

        // 凭证模板必配字段
        voucher.setBusinessType(BusinessType.SALE_ORDER);

        voucher.setScene(retailOrder.getScene());
        voucher.setOrigin(retailOrder.getId());
        voucher.setOriginBusinessType(retailOrder.getInnerBusinessType());
        voucher.setSource(pick.getId());
        voucher.setSourceBusinessType(pick.getInnerBusinessType());
        // 从表信息
        voucher.setVoucherItems(getSaleOrderVoucherItems(retailOrder, pick));

        return voucher;
    }

    public Set<VoucherItem> getSaleOrderVoucherItems(RetailOrder saleOrder, Pick pick) {
        Set<VoucherItem> voucherItems = new HashSet();
        Map<String, RetailOrderItem> saleOrderItemMap = saleOrder.getSaleItems().stream()
                .collect(Collectors.toMap(RetailOrderItem::getId, saleOrderItem -> saleOrderItem));

        pick.getPickItems().forEach(
                pickItem -> {

                    VoucherItem voucherItem = new VoucherItem();
                    RetailOrderItem saleOrderItem = saleOrderItemMap.get(pickItem.getOriginLine());
                    if (saleOrderItem == null) throw new RuntimeException("未找到零售订单行：" + pickItem.getOriginLine());

                    voucherItem.setProduct(pickItem.getProduct());
                    voucherItem.setTax(saleOrderItem.getTax());
                    voucherItem.setOwner(saleOrderItem.getLocation().getCompany());

                    // 设置为销售的单位以及单价
                    voucherItem.setUom(saleOrderItem.getUom());
                    voucherItem.setProductQty(pickItem.getQtyDone());
                    voucherItem.setPriceUnit(saleOrderItem.getPriceUnit());

                    voucherItem.setOriginItemId(saleOrderItem.getId());
                    voucherItem.setSourceItemId(String.valueOf(pickItem.getId()));
                    voucherItem.setSupplier(saleOrderItem.getSupplier());
                    voucherItems.add(voucherItem);
                }
        );
        return voucherItems;
    }


    /**
     * 获取 耗材领用 凭证
     * @param simpleRequest
     * @return
     */
    public Voucher flowConsumableUseVoucher(SimpleRequest simpleRequest) {
        Pick pick = simpleRequest.getO(Pick.class);
        ConsumableUse consumableUse = consumableUseRepository.findOne(pick.getOrigin());
        if (consumableUse == null) throw new RuntimeException(pick.getOrigin() + "耗材领用订单不存在！");
        Voucher voucher = new Voucher();
        // 主表信息
        voucher.setCompany(consumableUse.getCompany());
        voucher.setDept(consumableUse.getDept());
        voucher.setClerk(consumableUse.getClerk()); //业务员
        voucher.setPartner(consumableUse.getCompany().getParty().getPatyRoleByType("Customer"));

        voucher.setInventoryMode(InventoryMode.IM_OWN);     //设置库存模式：自有
        voucher.setAccountingMethod(AccountingMethod.AM_PURCHASE);  // 核算方式：经销
        voucher.setBusinessType(BusinessType.SALE_ORDER);
        voucher.setScene(consumableUse.getScene());
        voucher.setOrigin(consumableUse.getId());
        voucher.setOriginBusinessType(consumableUse.getInnerBusinessType());
        voucher.setSource(pick.getId());
        voucher.setSourceBusinessType(pick.getInnerBusinessType());
        // 从表信息
        voucher.setVoucherItems(getConsumableUseVoucherItems(consumableUse,pick));

        return voucher;
    }

    public Set<VoucherItem> getConsumableUseVoucherItems(ConsumableUse consumableUse, Pick pick) {
        Set<VoucherItem> voucherItems = new HashSet();
        Map<String, ConsumableUseItem> saleOrderItemMap = consumableUse.getConsumableUseItems().stream()
                .collect(Collectors.toMap(ConsumableUseItem::getId, Item -> Item));

        pick.getPickItems().forEach(
                pickItem -> {

                    VoucherItem voucherItem = new VoucherItem();
                    ConsumableUseItem OrderItem = saleOrderItemMap.get(pickItem.getOriginLine());
                    if (OrderItem == null) throw new RuntimeException("未找到零售订单行：" + pickItem.getOriginLine());

                    voucherItem.setProduct(pickItem.getProduct());
                    voucherItem.setTax(OrderItem.getProduct().getTax());
                    voucherItem.setOwner(consumableUse.getLocation().getCompany());

                    // 设置为销售的单位以及单价
                    voucherItem.setUom(OrderItem.getUom());
                    voucherItem.setProductQty(pickItem.getQtyDone());

                    voucherItem.setPriceUnit(0);
                    voucherItem.setOriginItemId(OrderItem.getId());
                    voucherItem.setSourceItemId(String.valueOf(pickItem.getId()));
                    voucherItem.setSupplier(null);
                    voucherItems.add(voucherItem);
                }
        );
        return voucherItems;
    }
}
